package com.yc.backstage.service.serviceImpl;

import com.yc.backstage.entity.SysUser;
import com.yc.backstage.service.SysMenuService;
import com.yc.backstage.service.SysPermissionService;
import com.yc.backstage.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @Classname SysPermissionServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/8/9 12:46
 * @Created by Mikechen
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    private SysMenuService SysMenuService;
    @Autowired
    private SysRoleService sysRoleService;
    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        //如果是admin用户
        if (user.isAdmin()) {
            perms.add("*:*:*");
        }else {
            perms.addAll(SysMenuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(sysRoleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }
}
